<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员充值</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
</head>
<body style="height: 770px">
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" class="input-text" id="staffId" name="staffId">
        <input type="hidden" class="input-text" id="cz1" name="cz1">
        <input type="hidden" class="input-text" id="cz2" name="cz2">
        <input type="hidden" class="input-text" id="cz3" name="cz3">
        <div class="layui-form-item">
            <label class="layui-form-label">卡号</label>
            <div class="layui-inline">
                <div class="layui-input-inline" >
                    <input type="text" id="cardNo" name="cardNo" class="layui-input" />
                </div>
                <div class="layui-input-inline" style="width: 300px;line-height: 38px;">
                    <span style="color: red;">请将工卡放入卡座，如未识别工卡，请重新放入</span>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">卡片状态</label>
            <div class="layui-input-block">
                <input type="text" id="cardStatus" name="cardStatus" disabled autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工编号</label>
            <div class="layui-input-block">
                <input type="text" id="bh" name="bh" disabled autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工姓名</label>
            <div class="layui-input-block">
                <input type="text" id="ygName" name="ygName" disabled autocomplete="off" class="layui-input">

            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="sex" disabled value="1" id="nan" title="男" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>男</div>
                <input type="radio" name="sex" disabled value="0" id="nv" title="女"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>女</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" id="phone" name="phone"  disabled autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <!--<div class="layui-form-item">
            <label class="layui-form-label">证件号码</label>
            <div class="layui-input-block">
                <input type="text" name="sfz" id="sfz" disabled autocomplete="off" class="layui-input">
            </div>
            &lt;!&ndash;<div class="layui-form-mid layui-word-aux">辅助文字</div>&ndash;&gt;
        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">数据库余额</label>
            <div class="layui-input-block">
                <input type="text" name="staffBalance" id="staffBalance" disabled autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">卡内余额</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="text" id="card" disabled autocomplete="off" class="layui-input">-->
<!--            </div>-->
<!--            &lt;!&ndash;<div class="layui-form-mid layui-word-aux">辅助文字</div>&ndash;&gt;-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">充值金额</label>
            <div class="layui-input-block">
                <input type="text" name="czAmount" id="czAmount" placeholder="请输入充值金额" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="cz" lay-submit lay-filter="cz">提交充值</button>
                <button class="layui-btn" id="cz_record" type="button" >充值记录</button>
                <button class="layui-btn" id="xf_record" type="button" >消费记录</button>
            </div>
        </div>
    </form>
</div>
<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/config.js"></script>
<script src="../../js/token.js"></script>
<script src="./js/lock.js"></script>
<script src="./js/smartReader.js"></script>
<script src="./js/card.js"></script>

<script>

    window.onload = function(){
        var $ = layui.jquery;
        $("input[name='cardNo']").focus() ;
        $.ajax({
            url:HOST+"cz/getUUid",
            success:function(res) {
                $("#cz1").val(res.data.cz);
                $("#cz2").val(res.data.czPre);
                $("#cz3").val(res.data.saveCz);
            }
        })
    }

    var $ = layui.jquery;
    var lalala = 0;
    getStaffInfo()
    var staffInfo = window.setInterval(getStaffInfo,3000);
    //获取用户信息
    function getStaffInfo(){
        var cardNo = $("#cardNo").val();
        // 请求用户信息
        $.ajax({
            url: HOST+"cz/getStaffInfo?cardNo=" + cardNo
            , type: "get"
            ,beforeSend: function(request) {
                request.setRequestHeader("token", layui.data('local_store').token);
            }
            , success: function (data) {
                if (data.code == 1 && lalala == 0){
                    lalala = 1;
                    window.clearInterval(staffInfo);
                    layer.msg("读取成功");
                    $("#staffId").val(data.data.staff.id);
                    $("#pin").val("");
                    $("#staffBalance").val(data.data.staff.cardBalance);
                    $("#ygName").val(data.data.staff.name);
                    var sex = data.data.staff.sex;
                    if(sex==1){
                        $("#nan").prop("checked",true);
                        form.render()
                    }else{
                        $("#nv").prop("checked",true);
                        form.render()
                    }
                    $("#phone").val(data.data.staff.phone);
                    $("#bh").val(data.data.staff.bh);
                    // $("#sfz").val(data.data.staff.sfz);
                    // $("#cls").val(data.data.staff.qiye.qyname);
                    $("#cardStatus").val("启用");
                    form.render();
                }else {
                    // layer.msg("卡号不存在！");
                    return;
                }
            }
        });
    }

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;

        //自定义验证规则  检测是否为数字
        form.verify({
            double: function(value){
                if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
                {
                    return '你输入的不是数字';
                }
            }
        });

        // async function chongzhi(map , money , success){
        //     reader.mifareCpuTransmit(icdev , map.select_file)
        //     await lock.wait()
        //     if(checkRs() != undefined) {
        //         layer.msg(checkRs())
        //         return false
        //     }
        //     reader.mifareCpuTransmit(icdev , map.select_dir)
        //     await lock.wait()
        //     if(checkRs() != undefined) {
        //         layer.msg(checkRs())
        //         return false
        //     }
        //     reader.mifareCpuTransmit(icdev , map.pin)
        //     await lock.wait()
        //     if(checkRs() != undefined) {
        //         layer.msg(checkRs())
        //         return false
        //     }
        //     // reader.mifareCpuTransmit(icdev , map.insert)
        //     // await lock.wait()
        //     // if(checkRs() != undefined) {
        //     //     layer.msg(checkRs())
        //     //     return false
        //     // }
        //     console.log("-------------------------")
        //     console.log(data.resultData)
        //     var cz1 = $("#cz1").val();
        //     map = undefined
        //     $.ajax({
        //         url:HOST+"cpu_card/chongzhi?uid="+$("#staffId").val()+"&info="+data.resultData+"&money="+money+"&czUUID="+cz1 ,
        //         beforeSend: function(request) {
        //             request.setRequestHeader("token", layui.data('local_store').token);
        //         },
        //         async:false ,
        //         success:function(res) {
        //             if(res.code == 600) {
        //                 layer.msg(data.msg);
        //             }else if(res.code == 200) {
        //                 map = res.data
        //             }
        //         }
        //     })
        //     // reader.mifareCpuTransmit(icdev , map.chongzhi)
        //     // await lock.wait()
        //     // if(checkRs() != undefined) {
        //     //     layer.msg(checkRs())
        //     //     return false
        //     // }
        //     // reader.rfHalt(icdev);
        //     // await lock.wait()
        //     if(success != undefined) {
        //         success()
        //     }
        // }

        //充值操作
        form.on('submit(cz)', function(data){
            form.render()
            var bh = $("#bh").val();
            var czAmount = $("#czAmount").val();
            var cardNo = $("#cardNo").val();
            var cz2 = $("#cz2").val();
            var cardStatus = $("#cardStatus").val();
            if(cardNo == null || cardNo == ""){
                layer.msg('请读取卡信息');
                return false;
            }else if (cardStatus != "启用") {
                // layer.msg('卡片已' + cardStatus + ',不能充值');
                return false;
            }else if (bh == null || bh == "") {
                layer.msg('没有关联用户，不能充值');
                return false;
            }else{
                if(czAmount == null || czAmount == "" || czAmount == 0 || czAmount*1 == "NaN"){
                    layer.msg('请输入充值金额');
                    return false;
                }else{
                    var money = Math.floor(czAmount*100);
                    data.field.staffId = staffId;
                    data.field.cardNo = cardNo;
                    var map = undefined
                    layer.confirm("请确认充值!" , function(){
                        $("#cz").addClass("layui-btn-disabled").attr("disabled",true);
                        // 请求充值预处理
                        // $.ajax({
                        //     url:HOST+"cpu_card/chongzhi_prepare?money="+money+"&czUUID="+cz2+"&cid="+uid ,
                        //     async:false ,
                        //     beforeSend: function(request) {
                        //         request.setRequestHeader("token", layui.data('local_store').token);
                        //     },
                        //     success:function(res){
                        //         if(res.code == 600) {
                        //             layer.msg(res.msg);
                        //         }else {
                        //             map = res.data
                        //         }
                        //     }
                        // })
                        // if(map == undefined) {
                        //     layer.msg("网络异常,无法完成充值")
                        //     return false
                        // }

                        var info = data.field;
                        // chongzhi(map , money , function(){
                            var cz3 = $("#cz3").val();
                            var m = money / 100
                            info.czAmount = m
                            info.staffId=$("#staffId").val()
                            console.log(info)
                            $.ajax({
                                url:HOST+"cz/saveCz?czUUID="+cz3
                                ,type:"POST"
                                ,contentType: 'application/json'
                                ,data:JSON.stringify(info)
                                ,success:function (data) {
                                    if(data.code == 600) {
                                        layer.msg(data.msg);
                                    }else if(data.code==0){
                                        layer.msg(data.msg);
                                    }else if(data.code==1){
                                        layer.close(layer.index);
                                        layer.msg('充值成功！');
                                        setTimeout(function(){
                                            window.location.reload();
                                        },1000);
                                    }else{
                                        layer.msg('拒绝访问');
                                    }
                                }
                                ,error:function () {
                                    console.log("ajax error");
                                }
                            });
                        // })
                    })
                }
            }
            return false;
        });

        // 充值记录
        $('#cz_record').on('click',function () {
            var staffId = $("#staffId").val();
            var cardNo = $("#cardNo").val();
            if(cardNo == null || cardNo == ""){
                layer.msg('请读取卡信息');
            }else {
                layer.open({
                    type:2
                    ,title:"充值记录"
                    ,skin:'layui-layer-molv'
                    ,area: ['95%', '95%']
                    ,content:(encodeURI("user_czjl.html?staffId=" + staffId))  //js获取url参数
                });
            }
        });
        // 消费记录
        $('#xf_record').on('click',function () {
            var staffId = $("#staffId").val();
            var cardNo = $("#cardNo").val();
            if(cardNo == null || cardNo == ""){
                layer.msg('请读取卡信息');
            }else {
                layer.open({
                    type:2
                    ,title:"消费记录"
                    ,skin:'layui-layer-molv'
                    ,area: ['95%', '95%']
                    ,content:(encodeURI("user_xfjl.html?staffId=" + staffId))  //js获取url参数
                });
            }
        });
    });
</script>
</body>
</html>